/**
 * parser
 */
Vpa.Parser = function() {
    this.comments = [];
    this.sources = {};
    this.parseComment();
    
    this.currentClass = {};
    this.classes = this.createClasses();
    this.buildDocInfo();
    var tree = new Vpa.Tree(this.classes);
    new Vpa.doc.Doc2File(this.classes);
    this.outputSourceFile();
};
Vpa.Parser.prototype = {
    outputSourceFile : function(){
        var _html = v8ReadFile(vpaConfig.sourceTpl);
        var sfs = this.sources;
        for(var i in sfs){
            var source = sfs[i];
            var content = source.getOutputText();
            var __html = _html.replace(/#\{vespa-code-source\}/,content);
            v8WriteFile(vpaConfig.outputSourceDir+source.name,__html);
        }
    },
    buildDocInfo : function(){
        var cmts = this.comments,
            classes = this.classes;
        for(var i=0,len=cmts.length;i<len;i++){
            var cmt = cmts[i];
            if(cmt.commentType == Vpa.Comment.commentType.CLASS){
                var className = cmt.getTag("class").className;
                this.currentClass = classes[className];
            }
            if(this.currentClass.addComment)
                this.currentClass.addComment(cmt);
        }
    },
    createClasses : function(){
        var cmts = this.comments,
            classes = {};
        /*
        here it needs a function to guess the class name when the class-name is not cleared;
         */
        for(var i=0,len=cmts.length;i<len;i++){
            var cmt = cmts[i];
            if(cmt.commentType == Vpa.Comment.commentType.CLASS){
                var className = cmt.getTag("class").className;
                if(!classes[className]){
                    classes[className] = new Vpa.doc.Class(className,this);
                }
            }
        }
        
        return classes;
    },
    _RegExp_Cmt : new RegExp(
        '\\/\\*\\*'
        + '(?:.|\\n)*?'
        + '\\*\\/',

        'g'
    ),
    parseFile : function(fileName){
        v8Log(fileName)
        var sourceFile = this.sources[fileName] = new Vpa.Source(fileName);
        var sourceFileName = sourceFile.name;
        var charset = v8System('file -ib '+fileName).replace(/.*?charset=(\S+)/,'$1').trim().toUpperCase();
        //if(vpaConfig.encoding)
        if('BINARY' != charset){
            v8Log(charset);
            v8System("enca -x UTF-8 "+ fileName);
        }

        var source = v8ReadFile(fileName);
        //replace the 
        source = source.replace(/\r\n/g,"\n").replace(/\r/g,"\n");
        var cmtArray = source.match(this._RegExp_Cmt),
            sourceArray = source.split(this._RegExp_Cmt);

        if(!cmtArray || cmtArray.length<=0)
            return;
        for(var i=0, len = cmtArray.length;i<len;i++){
            this.comments.push(
                new Vpa.Comment(
                    cmtArray[i],
                    sourceArray[i+1],
                    sourceFileName+"#vespa-"+i,
                    sourceFileName
                )
            );
        }

    },
    parseComment : function() {
        var source = "",
            fList = Vpa.System.getAllFiles(vpaConfig.source);
        
        for(var folder in fList){
            if(!fList[folder].isFolder){
                if(!(/\.js$/.test(foler)))
                    continue;
                if(!vpaConfig.exclude.test(folder))
                    this.parseFile(folder);
            }else{
                var clist = fList[folder].children;
                for(var i=0,len=clist.length;i<len;i++){
                    if(!(/\.js$/.test(clist[i])))
                        continue;
                    if(!vpaConfig.exclude.test(folder+"/"+clist[i]))
                        this.parseFile(folder+"/"+clist[i]);
                }
            }
        }
        
    }
};